package cn.wsalix.admin.repository;

import java.util.List;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import cn.anlaser.repository.RelatedRepository;
import cn.wsalix.admin.entity.RoleMenu;
import cn.wsalix.admin.entity.SysMenu;
import cn.wsalix.admin.entity.SysRole;
import cn.wsalix.admin.form.RoleMenuForm;

public interface RoleMenuRepository extends
		RelatedRepository<RoleMenuForm, RoleMenu, SysRole, SysMenu> {

	@Query("select rm.preFix from RoleMenu rm,SysRole r where rm.postFix.id = :menuId and rm.preFix.id = r.id ")
	public List<SysRole> findRolesByMenuId(@Param("menuId") Long menuId);

	@Query("select rm.postFix from RoleMenu rm,SysMenu m where rm.preFix.id = :roleId and rm.postFix.id = m.id")
	public List<SysMenu> findMensByRoleId(@Param("roleId") Long roleId);

	@Query("select rm from RoleMenu rm where rm.preFix.id = :roleId and rm.postFix.id = :menuId")
	public RoleMenu findRoleAndMenuOne(@Param("roleId") Long roleId,
			@Param("menuId") Long menuId);

	@Query("select rm from RoleMenu rm where rm.preFix.id = :roleId")
	public List<RoleMenu> findByRoleId(@Param("roleId") Long roleId);

	@Query("select rm from RoleMenu rm where rm.postFix.id = :menuId")
	public List<RoleMenu> findByMenuId(@Param("menuId") Long menuId);
	
	@Query("select rm.postFix from RoleMenu rm,UserRole ur where rm.preFix.id=ur.postFix.id and ur.preFix.id=:userId order by rm.createdDate")
	List<SysMenu> findByUserId(@Param("userId") Long userId);

}
